package com.zeei.ems.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()
                .title("应急平台api文档")
                .description("应急平台api文档")
//                .termsOfServiceUrl("/")
                .version("1.0")
                .build();
    }

    @Bean
    public Docket createRestApi(Environment environment) {
        // 设置要显示swagger的环境
        Profiles of = Profiles.of("dev", "test");
        // 判断当前是否处于该环境
//        boolean flag = environment.acceptsProfiles(of);

        boolean flag = true;
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("开发小组名称")
                .enable(flag)
                .select()
                // 配置要扫描接口的方式
                .apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.zeei.ems"))
                //.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                // 过滤什么路径 过滤只含有kong下面的请求
//                .paths(PathSelectors.ant("/**"))
                .paths(PathSelectors.any())
                .build()
//                .securitySchemes(unifiedAuth()) 统一认证
                .globalOperationParameters(defaultHeader()) //添加header
                ;
    }
    private static List<Parameter> defaultHeader(){
        /*ParameterBuilder appType = new ParameterBuilder();
        appType.name("app-type").description("应用类型").modelRef(new ModelRef("string")).parameterType("header").required(false).build();*/
        ParameterBuilder appToken = new ParameterBuilder();
        appToken.name("authToken").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        List<Parameter> pars = new ArrayList<>();
//        pars.add(appType.build());
        pars.add(appToken.build());
        return pars;
    }

    private static List<ApiKey> unifiedAuth() {
        List<ApiKey> arrayList = new ArrayList();
        arrayList.add(new ApiKey("app-type", "app-type", "header"));
        arrayList.add(new ApiKey("app-token", "app-token", "header"));
        return arrayList;
    }
}
